Skip to content

fix: preserve SavedRequest in auth success handler and fix deleteFile…#130

Merged
viktorlindell12 merged 4 commits into
mainfrom
fix/success-handler-saved-request
Apr 27, 2026
Merged

fix: preserve SavedRequest in auth success handler and fix deleteFile…#130
viktorlindell12 merged 4 commits into
mainfrom
fix/success-handler-saved-request

Conversation

@viktorlindell12

@viktorlindell12 viktorlindell12 commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

… condition

Summary by CodeRabbit

  • New Features

    • Improved upload flow: disables submit button during upload, shows progress text, submits form automatically after uploads, adds hidden file inputs for uploaded filenames, and uses updated upload/download endpoints.
  • Bug Fixes

    • Fixed file deletion confirmation so deletes occur only after user confirmation.
  • Refactor

    • Moved authentication-success redirect logic into a dedicated handler for cleaner security configuration.

@coderabbitai

coderabbitai Bot commented Apr 27, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@viktorlindell12 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 28 minutes and 32 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 239f1f51-48f5-4dd8-9ef5-7071a21be037

📥 Commits

Reviewing files that changed from the base of the PR and between e93db1d and d272085.

📒 Files selected for processing (1)
  • src/main/resources/static/js/script.js
📝 Walkthrough

Walkthrough

Extracted role-based redirect logic from SecurityConfig into a new SavedRequest-aware CustomAuthenticationSuccessHandler; updated client-side upload/delete JS to use new ticket upload endpoints, disable/restore submit button during uploads, append hidden uploaded filenames, submit form when no files selected, and require confirmation before delete.

Changes

Cohort / File(s) Summary
Auth handler
src/main/java/org/example/untitled/config/SecurityConfig.java, src/main/java/org/example/untitled/security/CustomAuthenticationSuccessHandler.java
SecurityConfig now returns CustomAuthenticationSuccessHandler; new handler extends SavedRequestAwareAuthenticationSuccessHandler, honors SavedRequest when present, otherwise maps authorities to Role and redirects to /admin, /handler, or /user.
Client upload/delete JS
src/main/resources/static/js/script.js
Updated endpoints from /upload/... to /tickets/upload/api/files/...; uploadNewFile() disables submit button, sets "Uploading..." text, appends hidden fileNames inputs for successful uploads, submits form after uploads (or if no files selected), and restores button on errors; deleteFile() now requires user confirmation before issuing delete request.

Sequence Diagram

sequenceDiagram
    actor User
    participant App as "Application / Login Controller"
    participant Spring as "Spring Security"
    participant Handler as "CustomAuthenticationSuccessHandler"
    participant Session as "HttpSession (SavedRequest)"
    participant Browser as "HttpServletResponse / Redirect"

    User->>App: POST /login (credentials)
    App->>Spring: Authenticate user
    Spring->>Handler: onAuthenticationSuccess(authentication)
    Handler->>Session: lookup SavedRequest
    alt SavedRequest exists
        Handler->>Spring: delegate to superclass (SavedRequest-aware)
        Spring->>Browser: redirect to original destination
    else No SavedRequest
        Handler->>Handler: map authorities -> Role (ADMIN/HANDLER/USER)
        Handler->>Handler: clear auth attributes
        Handler->>Browser: redirect to role-based URL (/admin,/handler,/user)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related issues

Suggested reviewers

  • FeFFe1996
  • apaegs

Poem

🐰 I hopped through the login gate and found the right way,

Saved routes now kept safe, no more stray,
Roles lead the tunnels to admin or user light,
Uploads hum along, buttons sleep through the night,
A tiny rabbit cheers — redirects done right! 🥕✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title specifically addresses the main changes: preserving SavedRequest in the authentication success handler and fixing deleteFile logic, matching the core work in SecurityConfig, CustomAuthenticationSuccessHandler, and script.js.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/success-handler-saved-request

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/main/resources/static/js/script.js (1)

66-71: ⚠️ Potential issue | 🟡 Minor

Guard against null response from apiReq.

apiReq returns null on 401/403 (line 86), so res.ok on line 67 will throw a TypeError in that scenario. Other call sites (e.g., fetchAFile, downloadFile, uploadNewFile) use if (!res) return;/continue; for this reason — deleteFile should follow the same pattern.

🛡️ Suggested guard
         const res = await apiReq(`/upload/api/files/delete-url?fileName=${encodeURIComponent(fileName)}`, {method: 'DELETE'});
+        if (!res) return;
         if(res.ok){
             await fetchAFile();
         } else {
             status.innerText= 'Error: ' + res.status;
         }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/resources/static/js/script.js` around lines 66 - 71, The deleteFile
call assumes apiReq returned a Response but apiReq can return null on 401/403;
update the deleteFile flow to guard for a falsy res (same pattern used in
fetchAFile/downloadFile/uploadNewFile) and exit early (return) if res is null
before accessing res.ok, then proceed to handle res.ok and error cases as
before.
🧹 Nitpick comments (1)
src/main/java/org/example/untitled/security/CustomAuthenticationSuccessHandler.java (1)

28-38: Consider role precedence instead of findFirst() when picking the redirect target.

Today CustomUserDetailsService assigns exactly one authority per user, so findFirst() is fine. If that ever changes (e.g. a user is granted both ROLE_USER and ROLE_ADMIN, or role hierarchy expansion gets applied), the order returned by Authentication#getAuthorities() is implementation‑defined and an ADMIN could end up redirected to /user. Selecting the highest‑privilege role explicitly makes the redirect deterministic and future‑proof.

Also, the default -> "/user" branch implicitly handles Role.USER. Listing USER explicitly would make the switch exhaustive over Role, so adding a new enum constant later forces the developer to revisit this code instead of silently routing to /user.

♻️ Proposed refactor
-        Role role = authentication.getAuthorities().stream()
-                .map(a -> Role.fromAuthority(a.getAuthority()))
-                .flatMap(Optional::stream)
-                .findFirst()
-                .orElse(Role.USER);
-
-        String targetUrl = switch (role) {
-            case ADMIN -> "/admin";
-            case HANDLER, SUPERVISOR -> "/handler";
-            default -> "/user";
-        };
+        Set<Role> roles = authentication.getAuthorities().stream()
+                .map(a -> Role.fromAuthority(a.getAuthority()))
+                .flatMap(Optional::stream)
+                .collect(Collectors.toUnmodifiableSet());
+
+        String targetUrl;
+        if (roles.contains(Role.ADMIN)) {
+            targetUrl = "/admin";
+        } else if (roles.contains(Role.HANDLER) || roles.contains(Role.SUPERVISOR)) {
+            targetUrl = "/handler";
+        } else {
+            targetUrl = "/user";
+        }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@src/main/java/org/example/untitled/security/CustomAuthenticationSuccessHandler.java`
around lines 28 - 38, The redirect logic in CustomAuthenticationSuccessHandler
uses findFirst() on authentication.getAuthorities(), which is order‑dependent;
instead collect all Roles via Role.fromAuthority(...) into a Set and determine
the highest‑privilege role explicitly (e.g., check for Role.ADMIN first, then
Role.SUPERVISOR, Role.HANDLER, then Role.USER) to pick the targetUrl
deterministically; also make the switch over Role exhaustive by listing USER
explicitly (or replace the switch with the explicit if/else precedence checks)
so adding new Role enum constants forces revisiting this code.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/main/resources/static/js/script.js`:
- Around line 66-71: The deleteFile call assumes apiReq returned a Response but
apiReq can return null on 401/403; update the deleteFile flow to guard for a
falsy res (same pattern used in fetchAFile/downloadFile/uploadNewFile) and exit
early (return) if res is null before accessing res.ok, then proceed to handle
res.ok and error cases as before.

---

Nitpick comments:
In
`@src/main/java/org/example/untitled/security/CustomAuthenticationSuccessHandler.java`:
- Around line 28-38: The redirect logic in CustomAuthenticationSuccessHandler
uses findFirst() on authentication.getAuthorities(), which is order‑dependent;
instead collect all Roles via Role.fromAuthority(...) into a Set and determine
the highest‑privilege role explicitly (e.g., check for Role.ADMIN first, then
Role.SUPERVISOR, Role.HANDLER, then Role.USER) to pick the targetUrl
deterministically; also make the switch over Role exhaustive by listing USER
explicitly (or replace the switch with the explicit if/else precedence checks)
so adding new Role enum constants forces revisiting this code.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f38c1c74-be72-4c26-8ca0-f5e7874793fc

📥 Commits

Reviewing files that changed from the base of the PR and between 5582853 and 95a17a1.

📒 Files selected for processing (3)
  • src/main/java/org/example/untitled/config/SecurityConfig.java
  • src/main/java/org/example/untitled/security/CustomAuthenticationSuccessHandler.java
  • src/main/resources/static/js/script.js

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/main/resources/static/js/script.js`:
- Around line 58-63: The frontend is appending multiple hidden inputs named
"fileNames" which do not bind to the backend DTO field
CreateCaseRequest.fileName, causing uploaded files to be orphaned; either change
the UI to only add a single hidden input named "fileName" (update the code that
creates inputs in the hidden-file-inputs container to set input.name =
'fileName' and ensure only the last/selected uploadedFileName is appended), or
extend the backend by changing CreateCaseRequest.fileName to List<String>
fileNames and update CaseService.createTicket and S3Service to accept and
persist multiple file names accordingly so that hidden inputs named "fileNames"
are properly bound.
- Around line 84-91: In deleteFile, res from apiReq can be null (apiReq returns
null on 401/403) so accessing res.ok may throw; update deleteFile to check for a
falsy res immediately after the await (e.g., if (!res) return or set an
error/exit) before touching res.ok, then proceed to call fetchAFile() or set
status when res.ok is true/false; reference the apiReq call and the surrounding
deleteFile function and fetchAFile to locate where to add the early null check
and return.
- Line 19: Client API calls currently use the `/tickets/upload/api/files/...`
prefix but S3RestController exposes endpoints at `/upload/api/files/...`; update
the JS calls (the apiReq invocations in script.js that build URLs like
`/tickets/upload/api/files/download-url`, `/tickets/upload/api/files/...`) to
remove the `/tickets` prefix so they call `/upload/api/files/...`
(alternatively, if you prefer server-side change, add a class-level
`@RequestMapping`("/tickets") to S3RestController), ensuring the paths used by
apiReq match the controller's routes.
- Around line 30-33: Replace direct form.submit() calls with
form.requestSubmit() so HTML5 validation and submit event listeners are
respected (change document.querySelector('form').submit() to const form =
document.querySelector('form'); form.requestSubmit()). In the async file
upload/submit flow, ensure the submit button (e.g., the variable used to disable
the button, such as submitButton or submitBtn) is re-enabled in all error
handlers and in a finally block so the UI is not left disabled on failure;
update the error callbacks around the file upload logic to call
submitButton.disabled = false (or the equivalent) and show an appropriate
user-facing message. Also ensure you preserve existing event listener behavior
by using requestSubmit() everywhere you previously called form.submit().
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 830355b8-cfce-434d-a81a-171b241a9e99

📥 Commits

Reviewing files that changed from the base of the PR and between 95a17a1 and e93db1d.

📒 Files selected for processing (1)
  • src/main/resources/static/js/script.js

Comment thread src/main/resources/static/js/script.js
Comment thread src/main/resources/static/js/script.js
Comment thread src/main/resources/static/js/script.js
Comment thread src/main/resources/static/js/script.js
@viktorlindell12
viktorlindell12 merged commit d0343e3 into main Apr 27, 2026
2 checks passed
@viktorlindell12
viktorlindell12 deleted the fix/success-handler-saved-request branch April 27, 2026 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants